package pers.vic.quartz.autoconfigure;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.sql.DataSource;

/**
 * 描述: 自定义的定时器配置
 * @see  org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration
 * @author Vic.xu
 * @date 2022-04-13 9:50
 */
@Configuration
@ConditionalOnExpression("${quartz.enabled:true}")
@EnableScheduling
@AutoConfigureAfter(QuartzAutoConfiguration.class)
@EnableConfigurationProperties(CustomizeQuartzProperties.class)
@MapperScan(basePackages = CustomizeQuartzAutoConfiguration.MAPPER_SCAN_PACKAGE, sqlSessionFactoryRef = CustomizeQuartzAutoConfiguration.SESSION_FACTORY_NAME)
@ComponentScan(basePackages = {CustomizeQuartzAutoConfiguration.COMPONENT_SCAN_PACKAGE, CustomizeQuartzAutoConfiguration.CONTROLLER_SCAN_PACKAGE})
public class CustomizeQuartzAutoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(CustomizeQuartzAutoConfiguration.class);

    /**
     * quartz中mapper接口的位置
     */
    public static final String MAPPER_SCAN_PACKAGE = "pers.vic.quartz.mapper";

    /**
     *  quartz中mapper.xml的位置
     */
    public static final String MAPPER_LOCATION = "classpath*:mapper/quartz/*.xml";


    /***
     * quartz中 service包的位置
     */
    public static final String COMPONENT_SCAN_PACKAGE = "pers.vic.quartz.service";

    /***
     * quartz中 controller包的位置
     */
    public static final String CONTROLLER_SCAN_PACKAGE = "pers.vic.quartz.controller";

    /**
     * 一个数据源，但是配置sqlSessionFactory 用于臊面当前mapper
     */
    public static final String SESSION_FACTORY_NAME = "sqlSessionFactory4Quartz";

    private CustomizeQuartzProperties customizeQuartzProperties;

    public CustomizeQuartzAutoConfiguration(CustomizeQuartzProperties customizeQuartzProperties) {
        this.customizeQuartzProperties = customizeQuartzProperties;
    }

    @Bean(name = SESSION_FACTORY_NAME)
    public SqlSessionFactory sqlSessionFactory4Quartz(DataSource dataSource) throws Exception {
        logger.info("register " + SESSION_FACTORY_NAME + "...........by vic");
        final SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        return factoryBean.getObject();
    }


}
